package sessionBean;

import entity.Question;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

@Stateless
@LocalBean
public class questionSessionBean {

    @PersistenceContext
    private EntityManager em;
    
    public Long createQuestion(String title, String instruction){
        System.out.println("Server:: Add task");
        Question q = new Question();
        q.setInstruction(instruction);
        q.setTitle(title);
        em.persist(q); 
        em.flush();
        return q.getId();
    }
    
    public Long editQuestion(Long questionId, String title, String instruction){
        Question q = em.find(Question.class, questionId);
        q.setInstruction(instruction);
        q.setTitle(title);
        em.persist(q); 
        em.flush();
        return null;
    }
    
    public Long deleteQuestion(Long questionId){
        Question q = em.find(Question.class, questionId);
        em.remove(q); 
        em.flush();
        return q.getId();
    }
    
    public Question searchQuestion(Long questionId){
        Question q = em.find(Question.class, questionId);
        return q;
    }
    
    public List getAllQuestions(){ //get all idea generation questions
       String qryStr = "SELECT q FROM Question q";
        Query query = em.createQuery(qryStr);
        return query.getResultList();
    }
}
